package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.page.PageResult;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.RequiredPermission;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;
    @RequestMapping("")
    public String index(){
        return "permission_list";
    }

    @RequestMapping("/list")
    @ResponseBody
    @RequiresPermissions("permission:list")
    @RequiredPermission("权限列表")
    public PageResult list(QueryObject qo){
        PageResult pageResult =  permissionService.query(qo);
        return pageResult;
    }

    @RequestMapping("/listAll")
    @ResponseBody
    public PageResult listAll(QueryObject qo){
        List<Permission> permissions = permissionService.listAll();
        PageResult pageResult = new PageResult(permissions.size(),permissions);
        return pageResult;
    }
    @RequestMapping("/reload")
    @ResponseBody
    public JSONResult reload(){
        JSONResult jsonResult = new JSONResult("加载成功");
        permissionService.reload();
        return jsonResult;
    }
}
